package com.hoperun.product.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.hoperun.product.base.MIP_BaseActivity;
import com.hoperun.product.entity.SpinnerEntity;
import com.hoperun.product.net.IServiceImpl;
import com.hoperun.product.net.RequestServiceImpl;
import com.hoperun.product.util.MD5Encrypt;
import com.hoperun.product.util.SharedPreUtil;
import com.hoperun.product.view.SimpleDialog;
import com.hoperun.yieldmgr.R;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class LoginConfirmSysActivity extends MIP_BaseActivity implements View.OnClickListener {

    private static int REQUEST_EXTERNAL_STRONGE = 1;
    JSONObject result=null;
    private Button loginBtn;
    private EditText accountEt;
    private EditText passwordEt;
    private Spinner mBmmcSp;
    //车间下拉框适配器
    private ArrayAdapter<String> mBmmcSpAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_confirm_sys);
        //initDate();
        initView();
        loginBtn.setOnClickListener(this);

    }



    private void initView() {
        loginBtn = (Button) findViewById(R.id.login_loginBtn);
        accountEt = (EditText) findViewById(R.id.login_accountEt);
        passwordEt = (EditText) findViewById(R.id.login_passwordEt);
        //mBmmcSp = (Spinner) findViewById(R.id.login_bmmcSp);
        // 设置车间下拉框适配器 数据来源于访问服务器的数据字典
//		mBmmcSpAdapter = new ArrayAdapter<String>(this, R.layout.spinner_item, mBmmcList);
//		mBmmcSpAdapter.setDropDownViewResource(R.layout.spinner_drop_item);
//		mBmmcSp.setAdapter(mBmmcSpAdapter);
//
        accountEt.setFocusable(true);
        accountEt.setFocusableInTouchMode(true);

        accountEt.requestFocus();
        accountEt.requestFocusFromTouch();
        accountEt.findFocus();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_loginBtn:
                startLogin();
                break;
        }
    }


    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        return super.dispatchKeyEvent(event);
    }

    /**
     * 开始登陆，正常手动输入
     */
    private void startLogin() {
        //accountEt.setText("admin");
        //passwordEt.setText("abc123");

        String account = accountEt.getText().toString().trim();
        String password = passwordEt.getText().toString().trim();

        //String password = MD5Encrypt.MD5(passwordEt.getText().toString().trim());
       // String bmmc="";
        //车间不是必选项
//		int position =mBmmcSp.getSelectedItemPosition();
//		if(position<0){
//			SimpleDialog.show(this, "登录失败，服务器连接异常！");
//			return;
//		}
//			bmmc = mSpinnerList.get(position).fieldValue;
//
        if (TextUtils.isEmpty(account)) {
            toast("请输入工号！");
            return;
        }
		if (TextUtils.isEmpty(password)) {
			toast("请输入密码！");
			return;
		}

        login(account, MD5Encrypt.MD5(password));
    }

    /**
     * 登录
     * @param account
     * @param password
     */
    private void login(String account,String password){
        showWait();
        RequestServiceImpl.login_huanpi(mMIPHandler, IServiceImpl.login_huanpi, account, password);
    }


    @Override
    public void onPostHandle(int requestType, Object objBody, boolean error) {
        stopWait();
        String opt = "";
        if (error) {
            switch (requestType) {
                case IServiceImpl.login_huanpi:
                    try {
                        // 需注意这里判断登录失败不能去解析json，服务端只会返回单字符串“FALSE”
                        JSONObject jsonObj = new JSONObject((String) objBody);
                        if (jsonObj.getInt("Code")==0) {


                            JSONObject userbeanObj = jsonObj.getJSONArray("Result").getJSONObject(0);
                            // 保存userbean数据到本地，格式为json字符串
                            SharedPreUtil.putValue(this, SharedPreUtil.USERHUANGBEAN, userbeanObj.toString());

                            Intent intent = new Intent(this, ManyConfirmActivity.class);
                            startActivity(intent);

                            // finish();
                        } else {
                            SimpleDialog.show(this, jsonObj.getString("Message"));
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                        toast("数据异常");
                    }
                    break;

            }
        } else {
            switch (requestType) {
                case IServiceImpl.LOGIN_R:
                    toast("登陆失败，请检查网络连接是否异常！");
                    break;

            }

        }
    }

}